var orderId = '';
var user;
var mRepairData;
apiready = function() {

	$api.getUserInf(function(userInf) {
		user = userInf;
	});


	orderId = api.pageParam.repairId;
	console.error(orderId);
	if (!orderId) {
		api.closeWin();
	}

	$api.do_post('/api/ropertyRepair/info', {
		body: JSON.stringify({
			id: orderId
		})
	}, function(repairData) {
		$api.log(repairData);
		mRepairData = repairData;
		if (repairData) {

			if (repairData.amount >= 0) {
				$('#paymentTotal').text('总计：￥' + repairData.amount);
			}
			var payCode = $("input[name='Pay']:checked").val();
			/*if(payCode=='1'){


			}else{
				$('#payAllBtn').remove();
			}
*/
			$('#postType').append(repairData.repairItem);
			$('#status').append(repairData.status);
			$('#amount').append(repairData.amount);
			$('#repairContent').append(repairData.repairContent);
			$('#confirmContent').append(repairData.repairContent);
			$('#confirmTime').append(repairData.repairContent);
			$('#repairMan').append(repairData.repairContent);
			$api.getNetPicPath(repairData.repairImg1, function(picPath) {
				$("#repair_pic").attr('src', picPath);
			});

			$api.getNetPicPath(repairData.repairImg2, function(mypicPath) {
				$("#repair_pic2").attr('src', mypicPath);
			});

			if (repairData.serviceType == "无偿") {
				$('#payAllBtn').val("立即确认");
				$("#selPayWay").remove();
			}

			if (!repairData.repairImg2) {
				alter('0000');

			}



		}
	})
};


//支付按钮
$('#payAllBtn').on('click', function() {
	api.showProgress({
	    style: 'default',
	    animationType: 'fade',
	    title: '努力加载中...',
	    text: '先喝杯茶...',
	    modal: false
	});
	var payWay = $('#Pay').val();
	console.error(payWay);
	if (!payWay) {
		$api.log("选择支付方式");
		return;
	}

	$api.do_post("/api/ropertyRepair/confirm", {
		body: JSON.stringify({
			repairId: orderId,
			account: user.account,
			payWay: payWay
		})
	}, function(ret, err) {
		if (ret.orderNo) {
			switch (payWay) {
				case '支付宝':
					alipay(ret.orderNo, mRepairData.amount);
					break;
				case '微信':
					wechatpay(ret.orderNo);
					break;
			}
		} else if (ret.msg == "确认成功") {
			api.hideProgress();
			$api.toastMsg("确认成功");
		} else {
			api.hideProgress();
			alert('该订单无法进行现金支付，请选择其他支付方式');
		}
	});
});



function alipay(orderNo, amount) {
	var aliPay = api.require('aliPay');
	aliPay.pay({
		subject: '支付',
		body: '支付宝支付',
		amount: amount,
		// amount: 0.01,
		tradeNO: orderNo
	}, function(ret, err) {
		api.hideProgress();
		switch (ret.code) {
			case '9000':
				{
					api.alert({
						title: '支付结果',
						msg: '支付成功',
						buttons: ['确定']
					});
					api.closeWin();
					break;
				}
			case '6001':
				{

					$api.toastMsg("支付已取消");
					break;
				}
			default:
				{
					api.alert({
						title: '支付结果',
						msg: ret.code + " 错误内容：" + err,
						buttons: ['确定']
					});
				}

		}
	});

}

function wechatpay(orderNo) {

	api.ajax({
		url: $api.getStorage('serviceUrl') + '/wxpay/app/order?orderNo=' + orderNo,
		method: 'get',
		timeout: 30,
		dataType: 'json',
		returnAll: false

	}, function(ret, err) {
		//			alert(JSON.stringify(ret));

		if (ret) {
			var wxPay = api.require('wxPay');

			wxPay.payOrder({
				apiKey: ret.appid,
				orderId: ret.prepayid,
				mchId: ret.partnerid,
				nonceStr: ret.noncestr,
				timeStamp: ret.timestamp,
				'package': 'Sign=WXPay',
				sign: ret.sign
			}, function(ret, err) {
				api.hideProgress();
				if (ret.status) {
					//支付成功
					api.alert({
						title: '支付结果',
						msg: '支付成功',
						buttons: ['确定']
					});

					api.closeWin();
				} else {
					if (err.code == -2) {
						api.alert({
							title: '支付结果',
							msg: '取消支付',
							buttons: ['确定']
						});
					} else {
						api.alert({
							title: '支付结果',
							msg: '支付失败',
							buttons: ['确定']
						});
					}
				}
			});

		} else {
			api.hideProgress();
			alert(err);
			api.toast({
				msg: '下单失败',
				duration: 1000,
				location: 'top'
			});
		}
	});
}